const Base = require('./base.js');

module.exports = class extends Base {
  indexAction(loginUser) {
    return this.success(loginUser);
  }

  // 更新用户名字
  async updateUserAction() {
    const user = await this.ctx.session('login');
    const { username } = this.post();
    if (!user || !user.id) {
      return this.fail('用户未登录，请先登录');
    }

    const userModel = think.model('user');
    const result = await userModel.where({ id: user.id }).update({ username });
    if (result) {
      return this.success({}, '姓名更新成功！');
    } else {
      return this.fail('更新失败，请重试！');
    }
  }

  async changeAvatarAction(loginUser) {
    const user = await this.ctx.session('login');
    if (!user || !user.id) {
      return this.fail('用户未登录，请先登录');
    }
    const input = {
      avatar: this.post('avatar')
    };
    const userModel = think.model('user');
    await userModel.where({ id: user.id }).update({ avatar: input.avatar });
    return this.success({}, '头像修改完成');
  }
};
